import { resolve, join } from 'path'
import { defineConfig, loadEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
  ElementPlusResolver,
} from 'unplugin-vue-components/resolvers'

import viteSvgIcons from 'vite-plugin-svg-icons';


// https://vitejs.dev/config/
export default defineConfig(({ mode }): UserConfig => {
  const env = loadEnv(mode, __dirname)
  return {
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ['vue', 'vue-router', '@vueuse/core'],
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dts: true,
        // 允许子目录作为组件的命名空间前缀。
        directoryAsNamespace: true
      }),
      viteSvgIcons({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: [
        // 添加别名
        { find: '@', replacement: join(__dirname, 'src') },
        { find: /* ~/ *//^~(?=\/)/, replacement: join(__dirname, 'node_modules') },
        { find: /* ~ *//^~(?!\/)/, replacement: join(__dirname, 'node_modules/') },
      ]
    },
    server: {
      host: "0.0.0.0",
      // 端口号
      port: 3000,
      // 允许跨域
      cors: true,
      // 自动打开浏览器
      open: true,
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_TARGET_API,
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/dev-api/, ""),
          rewrite: (path) => path.replace(env.VITE_BASE_API, ""),
        },
      }
    },
    build: {
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    }
  }
})


